/**
 * Created by Administrator on 2016/7/7.
 */
var user_token = sessionStorage.user_token;
//底部button固定
$("document").ready(function(){
    $("header img").click(function(){
        location.href = "consult.html";
    })
    var height = $(window).height();
    $(".button").css("top",height-190);
});

//调用默认

//请求医生信息
$(document).ready(function(){
    $("#pay").click(function(){
        var str = window.location.search;
        var order_num = str.replace(/[^0-9]+/g, '');
        if(order_num == undefined){
            order_num = sessionStorage.order_num ;
        }else{
            order_num = str.replace(/[^0-9]+/g, '');
        }
        sessionStorage.order_num = order_num;
        var minute =$(".count section:nth-child(1) span:nth-child(4)").text();
        minute = parseInt(minute);
        //强制ajax同步
        var check_type = sessionStorage.check_type;
        $.ajaxSettings.async=false;
        $.post(commonval+"/buy_order_time",{
            user_token:user_token,
            device:"wap",
            order_num:order_num,
            check_type:check_type,
            minute:minute
        },function(data){
            if(data.status == 1){
                location.href = "consult.html";
                createWindow("订单购买成功");
                function createWindow(msg) {
                    var window =$("<section class='showMsg'></section>");
                    var show = "<p>"+msg+"</p>";
                    window.append(show);
                    $("body").append(window);
                    setTimeout("$('.showMsg').remove()",3000);
                }
            }else{
                createWindow(data.msg);
                function createWindow(msg) {
                    var window =$("<section class='showMsg'></section>");
                    var show = "<p>"+msg+"</p>";
                    window.append(show);
                    $("body").append(window);
                    setTimeout("$('.showMsg').remove()",3000);
                }

            }
        });
    });
});
//请求医生信息
$(document).ready(function(){
    var str = window.location.search;
    var order_num = str.replace(/[^0-9]+/g, '');
    if(order_num == undefined){
        order_num = sessionStorage.order_num ;
    }else{
        order_num = str.replace(/[^0-9]+/g, '');
    }
    sessionStorage.order_num = order_num;
    //强制ajax同步
    var check_type = sessionStorage.check_type;
    $.ajaxSettings.async=false;
    $.post(commonval+"/orderInfo",{
        user_token:user_token,
        device:"wap",
        order_num:order_num,
        check_type:check_type
    },function(data){
        console.log(data.data.price);
        sessionStorage.tel_price = data.data.price;
        var docListMember = new Vue({
            el: '#order',
            data: data.data
        });
    });
});
//请求用户余额数据
var money;
var call_time =15-parseInt(sessionStorage.call_time);
console.log(sessionStorage.call_time);
$(document).ready(function(){
    var u_id = sessionStorage.u_id;
    $.post(commonval+"/user_info",{
        u_id:u_id,
        user_token:user_token,
        device:"wap",
    },function(data){
        money = data.data.money;
        //页面初始化数据加载
        $(".order section:nth-child(3) p").text("￥"+sessionStorage.tel_price+"/分钟");
        $(".count section:nth-child(1) span:last-child").text(call_time+"分钟");
        $(".count section:nth-child(2) p span:nth-child(1)").text(money);
        if(sessionStorage.tel_price*call_time>money){
            $("section>section:nth-child(2) p span:nth-child(3)").text("￥"+(sessionStorage.tel_price*call_time-money));
        }else{
            $("section>section:nth-child(2) p span:nth-child(3)").text(0);
        }
    })
});
//咨询时长 计算逻辑
var consultTime = 0
$(document).ready(function(){
    $(".count section:nth-child(1) img:nth-child(3)").click(function(e){
        e.stopPropagation();
        if(consultTime == 0){
            $(".count section:nth-child(1) img:nth-child(3)").attr("src","../images/minusSign.png");
        } else{
            --consultTime;

            $(".count section:nth-child(1) span:nth-child(4)").text(call_time+consultTime+"分钟");
            if(sessionStorage.tel_price*(call_time+consultTime)>money){
                $("section>section:nth-child(2) p span:nth-child(3)").text("￥"+(sessionStorage.tel_price*(call_time+consultTime)-money));
            }else{
                $("section>section:nth-child(2) p span:nth-child(3)").text(0);
            }
        }
    });
    $(".count section:nth-child(1) img:nth-child(2)").click(function(e){
        e.stopPropagation();
        $(".count section:nth-child(1) img:nth-child(3)").attr("src","../images/minusSign02.png");
        consultTime++;
        console.log(sessionStorage.tel_price);
        $(".count section:nth-child(1) span:nth-child(4)").text(call_time+consultTime+"分钟");
        if(sessionStorage.tel_price*(call_time+consultTime)>money){
            $("section>section:nth-child(2) p span:nth-child(3)").text("￥"+(sessionStorage.tel_price*(call_time+consultTime)-money));
        }else{
            $("section>section:nth-child(2) p span:nth-child(3)").text(0);
        }
    });
});
//充值跳转
//$(document).ready(function(){
//    var str = window.location.search;
//    var order_no = str.replace(/[^0-9]+/g, '');
//    var amountSource = $("section>section:nth-child(2) p span:nth-child(2)").text();
//    var amount = amountSource.replace(/[^0-9]+/g, '');
//    var u_id =sessionStorage.u_id;
//    var jump_url=sessionStorage.jump_url;
//    document.addEventListener('pingpp_one_ready',function(){
//        $("#pay1").click(function () {
//            pingpp_one.init({
//                app_id:'app_yffDSOj94WHSf104',                     //该应用在 ping++ 的应用 ID
//                order_no:'123123',                     //订单在商户系统中的订单号
//                amount: amount,                                   //订单价格，单位：人民币 分
//                // 壹收款页面上需要展示的渠道，数组，数组顺序即页面展示出的渠道的顺序
//                // upmp_wap 渠道在微信内部无法使用，若用户未安装银联手机支付控件，则无法调起支付
//                channel:['alipay_wap','wx_pub'],
//                charge_url:'http://dev.haotoufa.com/pay_charge',  //商户服务端创建订单的 url
//                charge_param:{ money:amount,order_num : order_no,u_id:u_id,jump_url:jump_url},    //(可选，用户自定义参数，若存在自定义参数则壹收款会通过 POST 方法透传给 charge_url)
//                open_id:'wx1234567890',                      //(可选，使用微信公众号支付时必须传入)
//                debug:false                                //(可选，debug 模式下会将 charge_url 的返回结果透传回来)
//            },function(res){
//                //debug 模式下获取 charge_url 的返回结果
//                if(res.debug&&res.chargeUrlOutput){
//                    console.log(res.chargeUrlOutput);
//                }
//                if(!res.status){
//                    //处理错误
//                    alert(res.msg);
//                }
//                else{
//                    //debug 模式下调用 charge_url 后会暂停，可以调用 pingpp_one.resume 方法继续执行
//                    if(res.debug&&!res.wxSuccess){
//                        if(confirm('当前为 debug 模式，是否继续支付？')){
//                            pingpp_one.resume();
//                        }
//                    }
//                    //若微信公众号渠道需要使用壹收款的支付成功页面，则在这里进行成功回调，
//                    //调用 pingpp_one.success 方法，你也可以自己定义回调函数
//                    //其他渠道的处理方法请见第 2 节
//                    else pingpp_one.success(function(res){
//                        if(!res.status){
//                            alert(res.msg);
//                        }
//                    },function(){
//                        //这里处理支付成功页面点击“继续购物”按钮触发的方法，
//                        //例如：若你需要点击“继续购物”按钮跳转到你的购买页，
//                        //则在该方法内写入 window.location.href = "你的购买页面 url"
//                        window.location.href='http://wap.haotoufa.com/html/paysuccess.html';//示例
//                    });
//                }
//            });
//        });
//    });
//});

//跳转支付
$(function(){
    $(".likeInput").click(function(){
        var amount = $(".count #likeInput p span:nth-child(3)").text();
        console.log(amount);
        location.href = "pay.html?amount="+amount;
        sessionStorage.jump_url =2;
    })
});

